const style = require('./render/style')
const Layout = require('./engine').default;

let __env = GameGlobal.wx || GameGlobal.tt || GameGlobal.swan;
let sharedCanvas  = __env.getSharedCanvas();
let sharedContext = sharedCanvas.getContext('2d');

let template
function draw() {
    Layout.clear();
    Layout.init(template, style);
    Layout.layout(sharedContext);
}

function anonymous(it ) { var out='<view class="container" id="main"> <view class="header"> <text class="title" value="排行榜"></text> </view> <view class="rankList"> <scrollview class="list"> ';var arr1=it.data;if(arr1){var item,index=-1,l1=arr1.length-1;while(index<l1){item=arr1[index+=1];out+=' ';if(index % 2 === 1){out+=' <view class="listItem listItemOld"> ';}out+=' ';if(index % 2 === 0){out+=' <view class="listItem"> ';}out+=' <view id="listItemUserData"> <text class="listItemNum" value="'+( index + 1)+'"></text> <image class="listHeadImg" src="'+( item.avatarUrl )+'"></image> <text class="listItemName" value="'+( item.nickname)+'"></text> </view> <text class="listItemScore" value="'+( item.rankScore)+' 分"></text> </view> ';} } out+=' </scrollview> <text class="listTips" value="仅展示前 '+( it.data.length)+' 位好友排名"></text> </view></view>';return out; }

function updateViewPort(data) { 
    Layout.updateViewPort({
        x: data.x,
        y: data.y,
        width: data.width,
        height: data.height,
    });
}
const w = wx.getSystemInfoSync().width
const h = wx.getSystemInfoSync().height
const r = wx.getSystemInfoSync().pixelRatio

function updateScore(data){
    wx.getUserCloudStorage({
        keyList:['score'],
        success(res){
            if(res.KVDataList.length>0){
                let resscore = res.KVDataList[0].value
                if(Number(data)>Number(resscore)){
                    updateFinalScore(data)
                    console.log('d')
                    console.log(data)
                }
            }else{
                updateFinalScore(data)
                console.log('x')
                console.log(data)
            }
            
            
            
        },
        fail(res){

        }
    })
}

function updateFinalScore(score){
    wx.setUserCloudStorage({
        KVDataList:[{key:'score',value:score}]
    })
}

function createrank(){

    let dataDemo = {
        data:[
           
        ],
    };

    wx.getFriendCloudStorage({
        keyList:['score'],
        success(res){
            let data = res.data
            for(let i = 0;i<data.length;++i){
                if(data[i].KVDataList.length>0){
                    let item = {}
                    item.rankScore = data[i].KVDataList[0].value;
                    item.avatarUrl = data[i].avatarUrl;
                    item.nickname = data[i].nickname;
                    dataDemo.data.push(item);
                }
            }
            dataDemo.data.sort((a, b) => b.rankScore - a.rankScore);
            template = anonymous(dataDemo)
        }
    })
}



__env.onMessage(data => {
    if ( data.type === 'engine') {
        if(data.event === 'viewport'){
            createrank()
            updateViewPort(data);
            setTimeout(()=>{
                draw()
            },1000)
        }
        if(data.event === 'updateScore'){
            updateScore(data.score)
            console.log(data.score)
        }
        if(data.event === 'getrank'){
            console.log(dataDemo)
        }
        
        
    }
    
    
});
